//落地页配置
spcialLinkConfit = {
    curLink:0,
    curContent:'',
    curOutIndex:0,
    specialLinkArr:[],  
    count:0,
    flag:false,// 分类数据没有最后一类数据标识
    linkInit:function(loadFn){
        var curDownLink = $('.specialLink');
        var that = this;     
        if(curDownLink.length>0){
            for(var i=0;i<curDownLink.length;i++){
                this.specialLinkArr.push({specialInd:'',specialContent:'',detailContent:''});
                var curLinkContObj = $(curDownLink[i]).parents('.select_list').next();
                curLinkContObj.attr('data-index',i).load('../special/special_link.html',function(){
                    that.count = that.count+1;
                    if(that.count == curDownLink.length && loadFn){
                        if(loadFn==1){
                            addScriptFn('../../../ht-js/operation-js/special/moban0'+curDownLink.length+'.js?v=@@hash');
                        }else if(loadFn == 7){
                            addScriptFn('../../../ht-js/operation-js/special/moban07.js?v=@@hash');
                        }else if(loadFn == 8){
                            addScriptFn('../../../ht-js/operation-js/special/moban08.js?v=@@hash');
                        } else if(loadFn == 9){
                            addScriptFn('../../../ht-js/operation-js/special/moban09.js?v=@@hash');
                        }else if(loadFn == 10){
                            addScriptFn('../../../ht-js/operation-js/special/moban010.js?v=@@hash');
                        }else{
                            addScriptFn('../../../ht-js/operation-js/special/moban04.js?v=@@hash');
                        }
                    }
                });                
            }
        }

        var that = this;
        if (window.attachEvent) {
            document.attachEvent("onclick", function(e){
              that.tagleShow(e);
              });
          } else if (window.addEventListener) {
              document.addEventListener('click',function(e){
                that.tagleShow(e);
            })
          }

        this.eventBind(); 

        
    },
    tagleShow:function(e){        
        if($(e.target).closest('.formControls').length<=0){
            $('.specContent').find('.guanlian').hide();
        }     
    }, 
    specialDefaultInit:function(){
        var curDownLink = $('.specialLink'); 
        var curValArr = this.specialLinkArr;   
        
        console.log(curValArr);
        console.log('反写');
        if(curValArr.length>0){
            for(var i=0;i<curValArr.length;i++){
                if(curValArr[i].specialInd != -1){
                    $('.specialLink').eq(i).find("option[value='"+curValArr[i].specialInd+"']").prop("selected", true);
                    var curValueObj = $('.specialLink').eq(i).parents('.select_list').next().find('.spe_item[data-url='+curValArr[i].specialInd+']');                  
                    console.log(curValueObj.length +'len');
                    if(curValueObj.length>0){
                        curValueObj.show().siblings().hide();                   
                        if(curValArr[i].specialInd == 4 || curValArr[i].specialInd == 5 || curValArr[i].specialInd == 7){
                            curValueObj.find('.input-link').val(curValArr[i].specialContent);
                        }else if(curValArr[i].specialInd == 1 || curValArr[i].specialInd == 3){
                            curValueObj.find('.input-text').attr('data-id',curValArr[i].specialContent).val(curValArr[i].detailContent);   
                        }else if(curValArr[i].specialInd == 2){                   
                            this.categorySetVal(curValArr[i].specialContent,curValArr[i].detailContent,curValueObj);
                        }else if(curValArr[i].specialInd == 6){
        
                        }
                    }else{
                       
                        console.log('没获取到元素');                      
                    }
                    
                }else{
                    $('.specialLink').eq(i).val('-1');
                    $('.specialLink').eq(i).parents('.select_list').next().find('.spe_item').hide();

                    var inputObj = $('.specialLink').eq(i).parents('.select_list').next().find('.spe_item');
                    inputObj.hide();
                    for(var i=0;i<inputObj.length;i++){
                        if(inputObj.find('.input-text').length>0){
                            inputObj.find('.input-text').val('').attr('data-id','');
                        }                        
                    }
                    
                }               
            }
        }
    },
    categorySetVal:function(specialContent,detailContent,curValueObj){
        var detailArr = detailContent.split(',');       
        this.linkGetCategory(0,1,curValueObj,detailArr[0]);
        console.log(detailArr.length+'tata');
        if(detailArr.length>=1){
            for(var k=0;k<detailArr.length;k++){
                var curLev = k+1;                
                this.linkGetCategory(curLev,detailArr[k],curValueObj,detailArr[curLev]);
            }
        }
    },  
    eventBind:function(){
        var that = this;
        $('.specialLink').on('change',function(){
            var specialInd = $(this).val();
            that.curOutIndex = Number($(this).attr('data-index')); 
            var curParentObj =  $(this).parent().parent().next();
            if(specialInd != that.specialLinkArr[that.curOutIndex].specialInd){
                that.specialLinkArr[that.curOutIndex].specialInd = specialInd;
                that.specialLinkArr[that.curOutIndex].specialContent="";
                that.specialLinkArr[that.curOutIndex].detailContent="";
            }           
           
            if(specialInd !=8 && specialInd !=-1){           
                curParentObj.find('.spe_item[data-url='+specialInd+']').show().siblings().hide();
            }else{
                curParentObj.find('.spe_item').hide();
                if(specialInd==8){
                    that.specialLinkArr[that.curOutIndex].specialContent = '';
                }
            }
            if(specialInd==2){
                that.linkGetCategory(0,1,curParentObj); 
            }
        });


        $('.specContent').on('change','select',function(){
            var curVal = $(this).val();
            var curLev = $(this).attr('data-ind');
            var curHtml =$(this).find("option:selected").text();
            var curIndex = that.curParentIndexGet($(this));
            if(curVal != -1){
                that.curContent = curVal;
                that.specialLinkArr[curIndex].specialContent = curVal;
                that.linkGetCategory(curLev,curVal,$(this).parents('.specCategory'));
            }
            if(curLev ==1){
                $(this).parent().siblings().find('select').html("<option value='-1'>请选择</option>");
                that.specialLinkArr[curIndex].specialContent = '';
                that.specialLinkArr[curIndex].detailContent = '';
            }else if(curLev ==2){
                $(this).parent().next().find('select').html("<option value='-1'>请选择</option>");
                that.specialLinkArr[curIndex].specialContent = '';
                that.specialLinkArr[curIndex].detailContent = '';
            }else if(curLev ==3){
                $(this).parent().next().find('select').html("<option value='-1'>请选择</option>");
            }
            that.categoryRem(curIndex,$(this));
            let categoryArr = that.specialLinkArr[curIndex].detailContent.split(',');
            that.specialLinkArr[curIndex].specialContent = categoryArr[categoryArr.length - 1];
            console.log(that.specialLinkArr);
        });

        $('.specContent').on('click','.chooseSku',function(){
            var curIndex = that.curParentIndexGet($(this));
            that.skuAddDialog(curIndex);
        });

        $('.specContent').on("click",'.getProuctTemplate',function(){               
            window.location.href = root.appversioncontrol.getProuctTemplate;
        });

        $('.specContent').on('click','.importProuctList',function(){
            $(this).next().trigger('click'); 
        });
        
        $('.specContent').on("click",'.downloadProductList',function(){ //下载商品集合   

           var curPrev = $(this).parents('.specContent').prev().find('.specialLink');
           var curType = curPrev.attr('datatype');
           var curId = curPrev.attr('dataid');
           var curInd = curPrev.attr('data-index');          
            if(curType && curId){
                if(curType == 1){
                    window.location.href = root.appversioncontrol.downloadProductList +'?modelId='+curId;
                }else{
                    window.location.href = root.appversioncontrol.downloadProductList +'?picId='+curId;
                }
            }         
            
        });
        
        $('.specContent').on('focus','.specialList',function(){
            that.specialListGet($(this).val(),$(this));
        });
       
        $('.specContent').on('input','.specialList',function(){               
            that.specialListGet($(this).val(),$(this));
        });       


        $('.specContent').on('click','.spec_li',function(){
            $(this).parent().hide();
            var curVal = $(this).html();
            var curSubId = $(this).attr('data-id');
            that.specialLinkArr[that.curParentIndexGet($(this))].specialContent = curSubId;
            that.specialLinkArr[that.curParentIndexGet($(this))].detailContent = curVal;          
            $(this).parent().prev().val(curVal).attr('data-id',curSubId);
        });


        //brand
        $('.specContent').on('focus','.brandSearch',function(){                
            if($(this).val() != ''){
                that.brandSearchFn($(this).val(),$(this));
            }else{
                $(this).next().hide();  
            }
        });

        $('.specContent').on('input','.brandSearch',function(){                
            if($(this).val() != ''){
                that.brandSearchFn($(this).val(),$(this));
            }else{
                $(this).attr('data-id','').next().hide(); 
                that.specialLinkArr[that.curParentIndexGet($(this))].specialContent = ''; 
                that.specialLinkArr[that.curParentIndexGet($(this))].detailContent = ''; 
            }
        });       


        $('.specContent').on('keydown','.brandSearch',function(e){    
            e = e || window.event;            
            switch(e.keyCode){                
                case 38:
                    that.brandKeyUp($(this));
                    break;
                case 40:
                    that.brandKeyDown($(this));
                    break;
                case 13:
                    $(this).next().hide();
                break;
            }  
        });
        
        $('.specContent').on('click','.brand_li',function(){
            var curObj = $(this);
            curObj.parent().prev().val(curObj.html()).attr({'data-id': curObj.attr('data-id'),'title': curObj.html()});
            that.specialLinkArr[that.curParentIndexGet($(this))].specialContent = curObj.attr('data-id'); 
            that.specialLinkArr[that.curParentIndexGet($(this))].detailContent = curObj.html();         
            curObj.parent().hide();
        });


        $('.specContent').on('blur','.input-link',function(){
            that.specialLinkArr[that.curParentIndexGet($(this))].specialContent = $(this).val();
            that.specialLinkArr[that.curParentIndexGet($(this))].detailContent = '';
        })        

    },
    categoryRem:function(indd,obj){
        var objList = obj.parents('.specCategory').find('select');
       var curDetail = [];
       for(var i=0;i<objList.length;i++){
            if(objList.eq(i).val()!= -1){
                curDetail.push(objList.eq(i).val());
            }else{
                break;
            }
       }
      
       this.specialLinkArr[indd].detailContent = curDetail.toString();
    },
    linkGetCategory:function(lev,id,curParent,checkId){              
        var parame = {};
        if(lev == 0){
            parame.categoryLevel = id;
        }else{
            parame.parentCateId = id;
        } 

        var selObj = curParent.find('select');
        for(var i=0;i<4;i++){
            if(i>lev){
                selObj.eq(i).html("<option value='-1'>请选择</option>");
            }
        }      

        web.doAjax(root.goodsInter.categoryByParam, 'post', parame, function(data){
            if (data != null && data.data.length > 0){ 
                var curHt = "<option value='-1'>请选择</option>";                   
                for (var i = 0; i < data.data.length; i++){
                    var checkStr = '';                   
                    if(checkId && checkId==data.data[i].category_id){
                        checkStr = 'selected';                                           
                    }                   
                    curHt+="<option value='" + data.data[i].category_id + "' "+checkStr+">" + data.data[i].category_name + "</option>";
                };
                curParent.find('select').eq(lev).html(curHt);
               // spcialLinkConfit.flag = true
            } else {
               // spcialLinkConfit.flag =false;
            }
        });
    },        
    skuAddDialog:function(curIndex){
        layer_show('选择spu', '../special/sku_add.html?index='+curIndex, "600", "300");
    },
    //专题活动数据获取
    specialListGet:function(curval,curObj){           
        var parame = {};
        var that = this;
        if(curval && curval.length>0){
            parame.subName = curval;
        }

        web.doAjax(root.appversioncontrol.searchAfterEasyWheelSubjectList, 'post', parame, function(data){
            if (data != null && data.data.length > 0){ 
                that.specialListInit(data.data,curObj,curval);
            }else{
                curObj.next().hide();
                curObj.attr('data-id','');           
                that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = ''; 
                that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = parame.subName; 
            }                
        });

        
    },       
    specialListInit:function(data,curObj,curval){    
        var that = this;      
        if(data.length<=0){
            curObj.next().hide();
        }
        var specHtml = "";  
        var count = 0;         
        for(var i=0;i<data.length;i++){
            specHtml+='<li class="spec_li" data-id="'+data[i].subId+'">'+data[i].subName+'</li>';

            if(curval == data[i].subName){
                that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = data[i].subId; 
                that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = data[i].subName; 
                curObj.attr('data-id',data[i].subId);
                count++;               
            }

        }
        curObj.next().html(specHtml).show();

        if(count==0){
            curObj.attr('data-id','');           
            that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = ''; 
            that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = curval; 
        }
    },
    brandSearchFn:function(val,curObj){
        var that = this;
        var parame = {
                brandName: val.trim()
            };
        web.doAjax(root.goodsInter.goods_getListBrand, 'post', parame, function(data){
            if (data != null && data.ListBrand.length > 0){                    
                var brandHtml = '';
                var count = 0;
                for(var i=0;i<data.ListBrand.length;i++){
                    var curDt = data.ListBrand[i];
                    brandHtml+='<li class="brand_li" data-id="'+curDt.brandId+'" title="' + curDt.brandName + '">'+curDt.brandName+ '</li>';

                    if(parame.brandName == curDt.brandName){
                        count++;
                        that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = data.ListBrand[i].brandId; 
                        that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = data.ListBrand[i].brandName; 
                        curObj.attr('data-id',data.ListBrand[i].brandId);                       
                     }
                     
                }
                curObj.next().html(brandHtml).show();

                if(count==0){
                    curObj.attr('data-id','');                
                    that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = ''; 
                    that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = parame.brandName;                     
                }

            }else{
                curObj.attr('data-id','');                
                that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = ''; 
                that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = parame.brandName; 
                curObj.next().hide();
            }                
        });

    },
    curParentIndexGet:function(_this){
        return Number(_this.parents('.specContent').attr('data-index'));
    },

    spuIdSubmit:function(index,value){       
        $('.specialLink').eq(index).parents('.select_list').next().find('.spe_item[data-url=4]').find('.input-link').val(value);
        this.specialLinkArr[index].specialContent = value;
        this.specialLinkArr[index].detailContent = '';        
    },
    brandKeyUp:function(_this){
        var nextOjb = _this.next();
        if(nextOjb.is(':visible')){
            var curChoose = nextOjb.find('.choosed');
            if(curChoose.length>0 && curChoose.prev().length>0){
                curChoose.prev().addClass('choosed').siblings().removeClass('choosed');
            }else{
                nextOjb.find('li:last').addClass('choosed').siblings().removeClass('choosed');
            }

            this.brandValueChoose(nextOjb.find('.choosed'));
            
        }        
    },
    brandKeyDown:function(_this){
        var nextOjb = _this.next();
        if(nextOjb.is(':visible')){
            var curChoose = nextOjb.find('.choosed');
            if(curChoose.length>0 && curChoose.next().length>0){
                curChoose.next().addClass('choosed').siblings().removeClass('choosed');
            }else{
                nextOjb.find('li').eq(0).addClass('choosed').siblings().removeClass('choosed');
            }

            this.brandValueChoose(nextOjb.find('.choosed'));
            
        }
    },
    brandValueChoose:function(curObj){
        if(curObj.length>0){
            var that = this;
            curObj.parent().prev().val(curObj.html()).attr('data-id',curObj.attr('data-id')); 
            that.specialLinkArr[that.curParentIndexGet(curObj)].specialContent = curObj.attr('data-id'); 
            that.specialLinkArr[that.curParentIndexGet(curObj)].detailContent = curObj.html();      
           
        }        
    },
    /*start*/
    // 条件判断
    conditionJudge:function(ary){
        var flag = true;
        for(var i = 0;i<ary.length;i++){
            var item = ary[i];
            //var itemAry = item.detailContent.split(',')
            // 判断图片是否为空
            if(item.url == ''){
                $.Huimodalalert('请填写必填项信息',2000);
                flag = false
                return
            }

            // id 为2时为分类，分类有3个和2个差别  --- 必选选到末级分类的情况判断
            if(item.id == 2 && item.detailContent == ''){
           /*     if(itemAry.length < 3 && spcialLinkConfit.flag){
                    $.Huimodalalert('请填写必填项信息',2000);
                    flag = false
                    return
                }*/
                $.Huimodalalert('请填写必填项信息',2000);
                flag = false
                return

            }else if(item.id == 3 && item.detailContent == ''){  //品牌页
                $.Huimodalalert('暂无该品牌信息，请重新输入',2000);
                flag = false
                return

            }else if(item.id == 4 && item.content == ''){ // 单品详情页
                $.Huimodalalert('请填写必填项信息',2000);
                flag = false
                return

            }else if(item.id == 1 && item.content == '' && item.detailContent == ''){ //专题列表
                $.Huimodalalert('请填写必填项信息',2000);
                flag = false
                return

            }else if(item.id == 5 && item.content == ''){ //自定义URL
                $.Huimodalalert('请填写链接地址',2000);
                flag = false
                return

            }else if(item.id == 6 && item.content == ''){ //商品集合
                $.Huimodalalert('请导入商品集合',2000);
                flag = false
                return

            }else if(item.id == 7 && item.content == ''){ //优惠券
                $.Huimodalalert('请填写电子券ID',2000);
                flag = false
                return

            }
        }
        return flag;
    },
    /*end*/

}


//批量导入商品 

function putDataToPageLink(_this){
    var formData = new FormData();         
    formData.append('file', $(_this)[0].files[0]);        
    $.ajax({
        url: root.appversioncontrol.importProuctList,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function (data) {
            $(_this).val('');               
            if(data.errorCode==0){ 
                spcialLinkConfit.specialLinkArr[spcialLinkConfit.curParentIndexGet($(_this))].specialContent = data.data;
                layer.msg('上传成功', {icon: 1, time: 1000}); 
            }else {
                $.Huimodalalert(data.msg,2000);
            }

        }
    });
};


function addScriptFn(fileUrl){
    var script=document.createElement("script");
    script.type="text/javascript";
    script.src=fileUrl;
    document.getElementsByTagName('body')[0].appendChild(script);  
}